package com.uploadtest.jiaotizhixing;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

class Worker implements Runnable {
    private int taskNumber;

    public Worker(int taskNumber) {
        this.taskNumber = taskNumber;
    }

    @Override
    public void run() {
        System.out.println("Executing task " + taskNumber);
    }
}

public class Main {
    public static void main(String[] args) {
        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        // 定义任务数量
        int numTasks = 10;

        // 提交任务并获取Future对象
        Future<?>[] futures = new Future<?>[numTasks];
        for (int i = 0; i < numTasks; i++) {
            Worker worker = new Worker(i);
            futures[i] = executorService.submit(worker);
        }

        // 等待任务按顺序完成
        for (Future<?> future : futures) {
            try {
                future.get();  // 等待任务完成
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 关闭线程池
        executorService.shutdown();
    }
}
